package com.yricky.indesk.ui.dialog

import android.app.Dialog
import android.content.Context
import androidx.activity.ComponentActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewTreeLifecycleOwner
import androidx.savedstate.ViewTreeSavedStateRegistryOwner
import com.yricky.indesk.R
import com.yricky.indesk.ui.compose.shapes.RoundRect

/**
 * @author Yricky
 * @date 2021/12/27
 */
fun showComposeDialog(context: Context, content:@Composable (Dialog)->Unit):Boolean{
    val activity: ComponentActivity = context as? ComponentActivity ?: return false
    val dialog: Dialog = Dialog(activity, R.style.dialog).apply {
        ViewTreeLifecycleOwner.set(window?.decorView ?:return false ,activity)
        ViewTreeSavedStateRegistryOwner.set(window?.decorView ?:return false ,activity)
        setContentView(ComposeView(activity).also {
            it.setContent{
                Box(modifier = Modifier
                    .background(Color(0xff000000),RoundRect.r(4))
                    .padding(4.dp)
                    .background(Color(0xffffffff))
                    .wrapContentSize()){
                    content(this@apply)
                }

            }
        })
    }
    dialog.show()
    return true
}